package lc20240517;


public class Demo2 {
    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(1, new TreeNode(2), new TreeNode(3));
        TreeNode treeNode1 = new TreeNode(1, new TreeNode(3), new TreeNode(4));
//        System.out.println(invertTree(treeNode1).left.val);
        System.out.println(isSymmetric(treeNode));
    }

    //反转二叉树
    public static TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode left = invertTree(root.left);
        TreeNode right = invertTree(root.right);
        root.left = right;
        root.right = left;
        return root;
    }

    //对称二叉树
    public static boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return checkTree(root.left, root.right);
    }

    public static boolean checkTree(TreeNode left, TreeNode right) {
        if (left == null && right == null) {
            return true;
        }
        if (left == null || right == null) {
            return false;
        }
        if (left.val != right.val) {
            return false;
        }
        return checkTree(left.left, right.right) && checkTree(left.right, right.left);

    }
}

